// pages/searchend/searchend.js
let { getsearch } = require("../../api/search")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search:[],
    value:null,
    searchhot:[],
    inputValue:null,
    searchData:null,
    Index:0,
    navdata:["综合","销量","价格","满意"]
  },
  searchFun(e){
    // console.log(e.detail.value);
    // 内容不能为空
    if(!e.detail.value){
      wx.showToast({
        title: '请输入搜索内容',
        icon:"none",
        duration:2000
      })
      return
    } 
    
  },
  navFun(e){
    console.log(e.currentTarget.dataset.index);
    this.setData({
      Index:e.currentTarget.dataset.index
    })
    if(e.currentTarget.dataset.index==0){
      this.setData({
        searchData:wx.getStorageSync('All')
      })
    }else if(e.currentTarget.dataset.index==1){
      for(let i =0;i<this.data.searchData.length;i++){
        for(let j=0;j<this.data.searchData.length-i-1;j++){
          if(Number(this.data.searchData[j].body.comments_total)<Number(this.data.searchData[j+1].body.comments_total)){
            let comments_total=this.data.searchData[j];
            this.data.searchData[j]=this.data.searchData[j+1];
            this.data.searchData[j+1]=comments_total;
          }
        }
      }
      this.setData(this.data);
    }else if(e.currentTarget.dataset.index==2){
      for(let i =0;i<this.data.searchData.length;i++){
        for(let j=0;j<this.data.searchData.length-i-1;j++){
          if(Number(this.data.searchData[j].body.price)>Number(this.data.searchData[j+1].body.price)){
            let price=this.data.searchData[j];
            this.data.searchData[j]=this.data.searchData[j+1];
            this.data.searchData[j+1]=price;
          }
        }
      }
      this.setData(this.data);
    }else if(e.currentTarget.dataset.index==3){
      for(let i =0;i<this.data.searchData.length;i++){
        for(let j=0;j<this.data.searchData.length-i-1;j++){
          if(parseFloat(this.data.searchData[j].body.satisfy_per)<parseFloat(this.data.searchData[j+1].body.satisfy_per)){
            let satisfy_per=this.data.searchData[j];
            this.data.searchData[j]=this.data.searchData[j+1];
            this.data.searchData[j+1]=satisfy_per;
          }
        }
      }
      this.setData(this.data);
    }
     
    
    console.log(this.data.index);
  },
  search(e){
//内容不能为空
  if(!e.detail.value){
    wx.showToast({
      title: '请输入内容',
      icon:"none",
      duration:2000
    })
    return
  }
  this.setData({
    value:e.detail.value
  })
  wx.setStorageSync('search_class', e.detail.value);
  // 将数据添加到数组中
  // 数组去从
  let con = this.data.search.findIndex(item=>item==e.detail.value);
  if(con==-1){
    this.data.search.unshift(e.detail.value);
    wx.setStorageSync('search', this.data.search)
  }
  console.log(this.data.search);

  wx.navigateTo({
    url: '/pages/searchend/searchend?vle='+e.detail.value,
  })
  this.setData({
    value:""
  })
  },
  searchFun1(e){
    // console.log(e.detail.value);
    // 获取输入框的值
    this.setData({
      inputValue:e.detail.value
    })
    console.log(this.data.inputValue);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showLoading({
      title: '加载中',
    })
    if(wx.getStorageSync('search_class')){
      this.setData({
        inputValue:wx.getStorageSync('search_class')
      })
    };
    if(wx.getStorageSync('search')){
      this.setData({
        search:wx.getStorageSync('search')
      })
    };
    // console.log(this.data.inputValue);
    // console.log(options.vle);
    getsearch({"query":options}).then(data=>{
      this.setData({
        searchData:data.data.list_v2
      })
      wx.setStorageSync('All', this.data.searchData);
      wx.hideLoading();
      console.log(this.data.searchData);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})